﻿#pragma once

#include <vector>
#include <map>
#include <mutex>
#include <thread>
#include <QString>
#include <EagleApi.h>
#include "user_define/SerialControl.h"
//#include "common.h"

typedef std::function<void(void)> exp_list_change_callback;
void  RegisterExpListChangeCallback(exp_list_change_callback cb);

typedef struct tagLineStatus
{
	bool m_bOPRet;
	eagle_camera_line_staus m_LineStatus;

}TLineStatus;

class CEagleDeviceMgr
{
public:
	CEagleDeviceMgr();
	virtual ~CEagleDeviceMgr();

public:
	void	ClearDevices();
	bool	RescanDevices(eagle_all_netcards *nics);
	bool	RescanDevices();
	bool	GetAllDevice(std::vector<int> &device);
	bool	GetCheckedDeviceList(std::vector<int> &device);
	bool	IsDeviceChecked(int device_id);
	std::vector<eagle_capture_config> GetCcdList(int device_id);
	std::vector<eagle_capture_config> GetCheckedCcdList(int device_id);

	
	QString GetDiskThreshold(int device_id) { return diskThreshold.at(device_id); }
	ECaptureStatusType GetCurrentDeviceCaptureStatus(int device_id);
	ECaptureStatusType GetCurrentSystemCaptureStatus();

	bool	SetDiskThreshold(int device_id, QString & newThreshold);
	void	SetCheckedDeviceList(std::vector<int> &device);
	bool	SetDeviceChecked(int device_id, bool bcheck);

	void	RegisterCcdListChangeCallback(exp_list_change_callback cb) { callback = cb; };

	void	ForceRefreshCallback();

	int		GetSdiMode(int device_id){ return SDI_Camera_mode_map.at(device_id); }
	void	SetSdiMode(int device_id, int type) { SDI_Camera_mode_map[device_id] = (PIXEL_SAMPLING_TYPE)type; }

	eagle_system_status GetDeviceStatus(int device_id); 
	eagle_camera_disk_config GetDeviceDiskConfig(int device_id); 
	eagle_capture_config GetDeviceCcdCaptureStatus(int device_id, int ccd_no);
	bool	GetOneDeviceConnectionStatus(int device_id);
	bool	GetConnectionStatus();
	void	CloseFetchThread(){ thread_destroyed = true; refresh_status_thread.join(); }

	void	MgrPauseStatusFetch() { fetch_thread_paused = true; }
	void	MgrResumeStatusFetch() { fetch_thread_paused = false; }
	bool   IsFrebOnLine(int devID,int ccdID,int& nRet);
	bool   GetAndSetPeerSerialCtrl(unsigned short& nValue, bool bToSet = true);
public:
	static CEagleDeviceMgr* GetInstance()
	{
		static CEagleDeviceMgr _Instance;
		return &_Instance;
	}
private:
	std::map<int, bool> device_check_list;
	std::vector<int> device_list;
	std::map<int, PIXEL_SAMPLING_TYPE> SDI_Camera_mode_map;
	std::map<int, QString> diskThreshold;
	std::mutex	device_lock;
	exp_list_change_callback callback;
	std::thread refresh_status_thread;
	std::map<int, eagle_system_status> device_system_status;
	std::map<int, std::map<int, eagle_capture_config>>	device_capture_status;
	std::map<int, TLineStatus> device_line_staus;  // new ADD [10/26/2016 ejitec]
	std::map<int, eagle_camera_disk_config> device_disk_config_status;
	bool thread_destroyed;
	bool fetch_thread_paused;
	std::mutex	device_status_lock;
	std::mutex	line_status_lock;
	std::map<int, std::tuple<bool,int>> device_connection_status;

private:
	void fetch_thread();
};

class EagleDataMgr
{
public:
	EagleDataMgr();
	~EagleDataMgr();

public:
	void AddUnfreeRecord(EagleData_Record* record){ record_list.push_back(record); }
	EagleData_Record* GetRecord(EagleData_Record_Id* id);
	EagleData_Record* GetRecord(std::wstring taskname, time_t start_time);
	EagleData_CcdRecord* GetCcdRecord(EagleData_Record_Id* id, int device_id, int serial);
	EagleData_CcdRecord* GetCcdRecord(std::wstring taskname, time_t start_time, int device_id, int serial);
	EagleData_CcdRecord* GetCcdRecord(EagleData_Record* task, int device_id, int serial);
	void ClearRecordList();

private:
	EagleData_Record* IsRecordExist(EagleData_Record_Id* id);

public:
	static EagleDataMgr* GetInstance()
	{
		static EagleDataMgr _Instance;
		return &_Instance;
	}
private:
	std::vector<EagleData_Record*> record_list;
};


ECaptureStatusType GetCurrentSystemStatus(bool GetReal);

ECaptureStatusType GetCurrentSystemStatus(int device_id);

bool GetAllDevice(std::vector<int> &device);

bool GetCheckedDeviceList(std::vector<int> &device);

std::vector<eagle_capture_config> GetCcdList(int device_id);

std::vector<eagle_capture_config> GetCheckedCcdList(int device_id);

int RescanDevices(eagle_all_netcards *nics);

void GetRealROIAndColorDepth(EDeviceType deviceType, int& column, int& line, int& color_depth);
void GetRealROIAndColorDepth(PIXEL_SAMPLING_TYPE SamplingType, int& column, int& line, int& color_depth);